/*
1. 客户端向服务端发起请求；
2. 服务端获取请求，在数据库商品集合中进行查询，将查询结果作为响应按商品分类返回给客户端；
3. 客户端获取响应，将响应数据渲染在页面中
*/
const Goods = require('../model/goods');

async function goodsList(ctx) {
	let result = {
		errCode: 0
	}

	let goods1 = await Goods.find({
		"goodType": 1
	});
	let goods2 = await Goods.find({
		"goodType": 2
	});
	let goods3 = await Goods.find({
		"goodType": 4
	});

	// console.log({
	// 	goods1,
	// 	goods2,
	// 	goods3
	// });

	// //查询到全部商品
	// let res = await Goods.find();

	// //通过filter过滤函数，将匹配要求的数据返回出来
	// let goods1 = res.filter(x => x.goodType == 1);
	// let goods2 = res.filter(x => x.goodType == 2);
	// let goods3 = res.filter(x => x.goodType == 4);

	//判断是否至少有一个分类中包含商品
	if (goods1.length == 0 && goods2.length == 0 && goods3.length == 0) {
		result.errCode = 1;
		result.errMsg = "查询不到任何商品"
		ctx.body = result;
	} else {
		result.errMsg = "已查询到商品"
		result.list = {
			type1: goods1,
			type2: goods2,
			type3: goods3
		};
		ctx.body = result;
	}
}

module.exports = goodsList;
